﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.Data;

namespace DigitalQuran.Research {

    public sealed class DataFrame : DataTable {

        public delegate object ItemModifierDelegate(object obj);

        public DataFrame(params string[] columns) {
            Exceptions = new List<Exception>();

            foreach (var col in columns) {
                this.Columns.Add(col, typeof(object));
            }
        }        

        void InsertRow(object[] items) {
            var r = base.NewRow();            
            r.ItemArray = items;            

            this.Rows.Add(r);
        }

        public void AddRow(params object[] items) {
            InsertRow(items);
        }

        public void AddEnumeratorRow(params object[] items) {            
            var row = new object[Columns.Count];
            for (int i = 0, j = 0; i < items.Length; i++, j++) {
                if (!(items[i] is IEnumerator))
                    row[j] = items[i];
                else {
                    var e = (IEnumerator)items[i];
                    while (e.MoveNext())
                        row[j++] = e.Current;
                    j--;
                }
            }            

            InsertRow(row);
        }

        public void AddEnumeratorRow(ItemModifierDelegate itemModifierOperation, params object[] items) {
            var row = new object[Columns.Count];
            for (int i = 0, j = 0; i < items.Length; i++, j++) {
                if (!(items[i] is IEnumerator))
                    row[j] = items[i];
                else {
                    var e = (IEnumerator)items[i];
                    while (e.MoveNext())
                        row[j++] = itemModifierOperation(e.Current);
                    j--;
                }
            }

            InsertRow(row);
        }

        public List<Exception> Exceptions { get; private set; }
    }
}